package com.retail.task;

import cn.hutool.core.date.DateUtil;
import com.retail.core.entity.ParamReq;
import com.retail.core.entity.enums.DelFlagEnum;
import com.retail.core.entity.enums.StatusFlagEnum;
import com.retail.core.task.RetailQuartzJobBean;
import com.retail.shop.service.RetailAdminShopService;
import com.retail.system.entity.request.AdminUpgradeConfigRequest;
import com.retail.system.entity.vo.AdminUpgradeConfigVo;
import com.retail.system.entity.vo.AdminVo;
import com.retail.system.enums.UserTypeEnums;
import com.retail.system.service.AdminService;
import com.retail.system.service.AdminUpgradeConfigService;
import com.retail.system.service.CronTriggersService;
import com.retail.system.service.CronTriggersSynchroTimeService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 等级晋升
 *
 * @author Administrator
 */
@Component
@Slf4j
public class AdminUpgradeLevelJobTask extends RetailQuartzJobBean {

    @Autowired
    private AdminService adminService;

    @Autowired
    private AdminUpgradeConfigService adminUpgradeConfigService;

    @Autowired
    private RetailAdminShopService retailAdminShopService;

    @Autowired
    private CronTriggersService cronTriggersService;

    @Autowired
    private CronTriggersSynchroTimeService cronTriggersSynchroTimeService;

    @Override
    protected CronTriggersService getCronTriggersService() {
        return cronTriggersService;
    }

    @Override
    protected CronTriggersSynchroTimeService getcronTriggersSynchroTimeService() {
        return cronTriggersSynchroTimeService;
    }

    @Override
    public void task(JobExecutionContext jobExecutionContext) {
        new Thread(() -> {
            // 查询所有的配置
            List<AdminUpgradeConfigVo> configs = findAdminUpgradeConfigs();

            List<AdminVo> lists = adminService.findAdminsByUserTypeAndAdminLevel(UserTypeEnums.BUSINESS_TYPE.getValue(), 1);

            for (AdminVo admin : lists) {
                // 查询数据统计
                Long upgradeLevel = admin.getUpgradeLevel();
                for (AdminUpgradeConfigVo config : configs) {
                    if (config.getId() > upgradeLevel) {
                        Integer count = retailAdminShopService.findModelsCountByAdminIdAndUpgradeDays(admin.getId(), config.getUpgradeDay());
                        if (count > config.getShopCount()) {
                            upgradeLevel = config.getId();
                        }
                    }
                }

                if (upgradeLevel > admin.getUpgradeLevel()) {
                    // 更新权益等级
                    admin.setUpgradePreLevel(admin.getUpgradeLevel());
                    admin.setUpgradeLevel(upgradeLevel);
                    admin.setUpgradeTime(DateUtil.format(new Date(), "yyyy-MM-dd"));
                    adminService.updateUpgradeLevel(admin);
                }
            }
        }).start();
    }

    // 晋升配置
    private List<AdminUpgradeConfigVo> findAdminUpgradeConfigs() {
        ParamReq<AdminUpgradeConfigRequest> paramReq = new ParamReq<>();
        paramReq.setCurrent(1);
        paramReq.setSize(1000);

        AdminUpgradeConfigRequest request = new AdminUpgradeConfigRequest();
        request.setStatusFlag(StatusFlagEnum.USE.getValue());
        request.setDelFlag(DelFlagEnum.NO.getValue());
        paramReq.setEntity(request);
        List<AdminUpgradeConfigVo> lists = adminUpgradeConfigService.listPage(paramReq);
        return lists;
    }
}
